define(['jquery'], function(require, factory) {
    class Header {
        constructor () {
            this.loadHTML().then(() => {
            this.calcCartCount()
            this.addfixednav()
            })
        }
        loadHTML () {
            // 把异步代码写成同步形式，套路就是 return new Promise
            return new Promise(resolve => {
              // $('header').load('/html/modules/header.html', () => {
              //   resolve()
              // })
      
              // 异步加载成功以后执行resolve
              $('header').load('/html/modules/header.html', resolve)
            })
          }
        calcCartCount () {
            let cart = localStorage.getItem('cart')
            let count = 0
            if (cart) {
                cart = JSON.parse(cart)
                count = cart.reduce((num, shop) => {
                    return num += shop.count
                }, 0)
            }
            $('#Num').html(count)
        }
        addfixednav () {
            var nav=$(".banner2");
            var win=$(window);
            var sc=$(document);
            win.scroll(function(){
                if(sc.scrollTop()>=212){
                    nav.addClass("fixednav");
                }else{
                    nav.removeClass("fixednav");
                }
            })
        }
        dlzc () {
            const dr = $('#dr')
            const dc = $('#dc')
            const username = $.cookie('username');
            if(username) {
                dr.addClass('hid')
                dc.removeClass('hid')
            }
            dc.on('click', () => {
                if(confirm('确定要登出吗？')){
                    $.cookie('username', null);
                    dr.removeClass('hid')
                    dc.addClass('hid')
                }
            })
        }
    }
    return new Header()
});


